package m1;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Consumer {

    public static void main(String[] args) throws IOException, TimeoutException {

        //1. 连接
        ConnectionFactory f = new ConnectionFactory();
        f.setHost("192.168.64.140");
        f.setPort(5672);
        f.setUsername("admin");
        f.setPassword("admin");
        Channel c = f.newConnection().createChannel();

        //2. 创建helloworld队列
        // 生产者和消费者都创建同一个队列，不必关心他们的启动顺序
        c.queueDeclare("helloworld", false, false, false, null);

        //2.1 创建回调对象
        DeliverCallback deliverCallback = new DeliverCallback() {
            @Override
            public void handle(String s, Delivery delivery) throws IOException {

                byte[] a = delivery.getBody();
                String s1 = new String(a);
                System.out.println("收到：" + s1);

            }
        };
        CancelCallback cancelCallback = new CancelCallback() {
            @Override
            public void handle(String s) throws IOException {

            }
        };

        //3. 从队列接收消息
        c.basicConsume("helloworld", true,deliverCallback,cancelCallback);

    }

}
